<%-- 
    Document   : circleInfo
    Created on : Dec 6, 2011, 3:31:44 PM
    Author     : Michael
--%>

<%@page import="java.util.Random"%>
<%@page import="java.sql.*"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<% if(session.getAttribute("LoggedIn")==null)
       {
    response.sendRedirect("index.html");
    return;
        }

%>
<%
    if(!session.getAttribute("Access").equals("Customer"))
        response.sendRedirect("gohome.jsp");
    Connection conn = null;
    try
    {
        conn = new DBClasses.GetConnection().getConnection();
        Statement stmt = conn.createStatement();
        
        int circleID = Integer.parseInt(request.getParameter("CIRC"));
        
        if(circleID==-1)
        {
          boolean exists = true;
          while(exists)
          {
              Random r = new Random();
              r.setSeed(System.currentTimeMillis());
              circleID = r.nextInt(1000000);
              ResultSet rs = stmt.executeQuery("SELECT CircleID FROM circle WHERE CircleID='" + circleID + "'");
              if(!rs.next())
                  exists=false;
          }
          
          java.sql.PreparedStatement ps = conn.prepareStatement("INSERT INTO circle "
                  + "(CircleID, CircleName, CircleType, CircleOwner)"
                  + " VALUES (?, ?, ?, ?)");
          
          ps.setInt(1, circleID);
          ps.setString(2, request.getParameter("circlename"));
          ps.setString(3, request.getParameter("circletype"));
          ps.setInt(4, Integer.parseInt(session.getAttribute("UserID").toString()));
          
          System.out.println(ps.toString());
          ps.execute();
          conn.commit();
          
          int pageID = -1;
          exists = true;
          while(exists)
          {
              Random r = new Random();
              r.setSeed(System.currentTimeMillis());
              pageID = r.nextInt(1000000);
              ResultSet rs = stmt.executeQuery("SELECT PageID FROM pages WHERE PageID=" + pageID);
              if(!rs.next())
                  exists=false;
          }
          
          ps = conn.prepareStatement("INSERT INTO pages "
                  + "(PageID, PostCount, AssociatedCircleID)"
                  + " VALUES (?, ?, ?)");
          
          ps.setInt(1, pageID);
          ps.setInt(2, 0);
          ps.setInt(3, circleID);
          
          ps.execute();
          conn.commit();
          
          conn.createStatement().execute("INSERT INTO circlemembership (CustomerID, CircleID) VALUES (" + 
                  Integer.parseInt(session.getAttribute("UserID").toString()) + ", " + circleID + ")");
          
        }
        
        else
        {
            conn = new DBClasses.GetConnection().getConnection();
            stmt = conn.createStatement();
            String query = "UPDATE circle SET CircleName='" + request.getParameter("circlename") + "', " +
                    "CircleType='" + request.getParameter("circletype") + "' WHERE CircleID=" + request.getParameter("CIRC");
            System.out.println(query);
            stmt.execute(query);
        }
    }
    catch(Exception e) { e.printStackTrace();  response.sendRedirect("gohome.jsp"); }
    
    finally
                       {
        try
        {
            conn.commit();
            conn.close();
            response.sendRedirect("gohome.jsp");
        }
        catch(Exception e) { response.sendRedirect("gohome.jsp"); }
    }
%>
